package com.aiti.lulian.service.impl;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardRichTextDto;
import com.aiti.lulian.entity.CardRichText;
import com.aiti.lulian.entity.CardRichTextExtension;
import com.aiti.lulian.mapper.CardRichTextMapper;
import com.aiti.lulian.service.CardRichTextService;
import com.aiti.lulian.service.ICardRichTextExtensionService;
import com.aiti.lulian.vo.CardRichTextVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CardRichTextServiceImpl extends BaseSerivceImpl<CardRichTextMapper, CardRichText> implements CardRichTextService {

    @Autowired
    private CardRichTextMapper cardRichTextMapper;

    @Autowired
    private ICardRichTextExtensionService cardRichTextExtensionService;

    @Override
    public CardRichTextVo getCardRichTextByEnterpriseId(String enterpriseId,String flag) {
        //根据企业ID查询相应的楼层数据
        LambdaQueryWrapper<CardRichText> richTextQueryWrapper = new LambdaQueryWrapper<>();
        richTextQueryWrapper.eq(CardRichText::getEnterpriseId, enterpriseId);
        //如果Flag是空的话，表示是在小程序端访问的，这里只查询可以显示的楼层数据
        if(StringUtils.isBlank(flag)){
            richTextQueryWrapper.eq(CardRichText::getIsShow,"0");
        }
        List<CardRichText> cardRichTexts = cardRichTextMapper.selectList(richTextQueryWrapper);
        cardRichTexts.sort((a, b) -> {
            int aSort = a.getSort() == null ? Integer.MAX_VALUE : Integer.parseInt(a.getSort());
            int bSort = b.getSort() == null ? Integer.MAX_VALUE : Integer.parseInt(b.getSort());
            return aSort - bSort;
        });

        //根据企业ID查询相应的楼层扩展数据
        LambdaQueryWrapper<CardRichTextExtension> extensionUrlQueryWrapper = new LambdaQueryWrapper<>();
        extensionUrlQueryWrapper.eq(CardRichTextExtension::getEnterpriseId, enterpriseId);
        extensionUrlQueryWrapper.eq(CardRichTextExtension::getType, "1");
        CardRichTextExtension extensionUrl = cardRichTextExtensionService.getOne(extensionUrlQueryWrapper);

        LambdaQueryWrapper<CardRichTextExtension> extensionPicQueryWrapper = new LambdaQueryWrapper<>();
        extensionPicQueryWrapper.eq(CardRichTextExtension::getEnterpriseId, enterpriseId);
        extensionPicQueryWrapper.eq(CardRichTextExtension::getType, "2");
        List<CardRichTextExtension> extensionPicList = cardRichTextExtensionService.list(extensionPicQueryWrapper);
        List<String> picUrlList = extensionPicList.stream().map(i -> i.getContent()).collect(Collectors.toList());

        return CardRichTextVo.builder()
                .cardRichTexts(cardRichTexts)
                .url(extensionUrl == null ? "" : extensionUrl.getContent())
                .pics(picUrlList)
                .build();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message addCardRichTextByEnterpriseId(CardRichTextDto cardRichTextDto) {
        //存储楼层富文本
        List<CardRichText> cardRichTexts = cardRichTextDto.getCardRichTexts();
        boolean saveResult1 = this.saveBatch(cardRichTexts);

        //存储超链接以及图片
        ArrayList<CardRichTextExtension> list = new ArrayList<>();
        String enterpriseId = cardRichTexts.get(0).getEnterpriseId();
        String url = cardRichTextDto.getUrl();
        if (StrUtil.isNotBlank(url)) {
            CardRichTextExtension extensionUrl = CardRichTextExtension.builder()
                    .content(url)
                    .type("1")
                    .enterpriseId(enterpriseId)
                    .build();
            list.add(extensionUrl);
        }
        List<String> pics = cardRichTextDto.getPics();
        if (pics != null && pics.size() > 0) {
            for (String pic : pics) {
                CardRichTextExtension extensionPic = CardRichTextExtension.builder()
                        .content(pic)
                        .type("2")
                        .enterpriseId(enterpriseId)
                        .build();
                list.add(extensionPic);
            }
        }
        boolean saveResult2 = cardRichTextExtensionService.saveBatch(list);

        if (saveResult1) {
            return Message.success("添加成功");
        } else {
            return Message.fail("添加失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message updateCardRichTextByEnterpriseId(CardRichTextDto cardRichTextDto) {
        List<CardRichText> cardRichTexts = cardRichTextDto.getCardRichTexts();
        String enterpriseId = cardRichTexts.get(0).getEnterpriseId();

        //根据企业ID批量删除原有楼层数据
        LambdaQueryWrapper<CardRichText> richTextQueryWrapper = new LambdaQueryWrapper<>();
        richTextQueryWrapper.eq(CardRichText::getEnterpriseId, enterpriseId);
        cardRichTextMapper.delete(richTextQueryWrapper);

        //存储楼层富文本
        boolean saveResult1 = this.saveBatch(cardRichTexts);

        //根据企业ID批量删除原有楼层扩展数据
        LambdaQueryWrapper<CardRichTextExtension> extensionQueryWrapper = new LambdaQueryWrapper<>();
        extensionQueryWrapper.eq(CardRichTextExtension::getEnterpriseId, enterpriseId);
        cardRichTextExtensionService.remove(extensionQueryWrapper);

        //存储超链接以及图片
        ArrayList<CardRichTextExtension> list = new ArrayList<>();
        String url = cardRichTextDto.getUrl();
        if (StrUtil.isNotBlank(url)) {
            CardRichTextExtension extensionUrl = CardRichTextExtension.builder()
                    .content(url)
                    .type("1")
                    .enterpriseId(enterpriseId)
                    .build();
            list.add(extensionUrl);
        }
        List<String> pics = cardRichTextDto.getPics();
        if (pics != null && pics.size() > 0) {
            for (String pic : pics) {
                CardRichTextExtension extensionPic = CardRichTextExtension.builder()
                        .content(pic)
                        .type("2")
                        .enterpriseId(enterpriseId)
                        .build();
                list.add(extensionPic);
            }
        }
        boolean saveResult2 = cardRichTextExtensionService.saveBatch(list);

        if (saveResult1) {
            return Message.success("更新成功");
        } else {
            return Message.fail("更新失败");
        }
    }

    @Override
    public Message deleteCardRichTextByEnterpriseId(String baseId) {
        LambdaQueryWrapper<CardRichText> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CardRichText::getBaseId,baseId);
        int delete = cardRichTextMapper.delete(queryWrapper);
        if (delete>0) {
            return Message.success("删除成功");
        } else {
            return Message.fail("删除失败");
        }
    }
}

